package leetcode.medium.hIndex;

import java.util.*;

/**
 * 求 h 指数，题目的链接：<a href="https://leetcode.cn/problems/h-index"/>链接</a>
 */
public class Solution {
    public int hIndex(int[] citations) {
        int ans = countMethod(citations);
        return ans;
    }

    private int countMethod(int[] citations) {
        Arrays.sort(citations);
        int h = 0, i = citations.length - 1;
        while (i >= 0 && citations[i] > h) {
            h++;
            i--;
        }
        return h;
    }

    private int bruteForce(int[] citations) {
        Arrays.sort(citations);
        for (int i = citations.length; i >= 1 ; i--) {
            int count = 0;
            for (int j = 0; j < citations.length; j++) {
                if (citations[j] >= i) {
                    count++;
                    if (count >= i) {
                        return count;
                    }
                }
            }
        }
        return 0;
    }
}
